﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using SCResources.General;

namespace SCResources.DataLayer
{
    internal class IO_FileMoving
    {
        private FileInfo[] files;
        private DirectoryInfo directoryInfo;

        public IO_FileMoving(string root)
        {
            directoryInfo = new DirectoryInfo(root);
            //todo: get extentions from user
            FileInfo[] filesMkv = directoryInfo.GetFiles("*.mkv", SearchOption.AllDirectories);
            FileInfo[] filesAvi = directoryInfo.GetFiles("*.avi", SearchOption.AllDirectories);

            files = new FileInfo[filesMkv.Length + filesAvi.Length];
            Array.Copy(filesMkv, files, filesMkv.Length);
            Array.Copy(filesAvi, files, filesAvi.Length);
        }

        /// <summary>
        /// move matched files (mkv and avi)
        /// </summary>
        /// <param name="searchKey">searched string</param>
        /// <param name="destenation">destenation path</param>
        /// <param name="root">root folder to search in</param>
        /// <returns></returns>
        public FileMovingResult moveFiles(List<FileMovingSerieObject> moveAbleSeries)
        {

            foreach(FileMovingSerieObject serie in moveAbleSeries)
            {
                FileMovingResult returnResult = new FileMovingResult();
                string[] tempParts = serie.Title.Split(' ');

                //Split the title of the serie on spaces, and se if the file contains every part
                //ex. The mentalist would be => "The" and "mentalist"
                //if those 2 parts are containing in the file name, it will be an episode of that serie
                //notice that this is case sensitiv therefor ToLower();
                foreach (FileInfo file in files)
                {
                    bool move = false;
                    for (int i = 0; i < tempParts.Length; i++)
                    {
                        if (file.FullName.ToLower().Contains(tempParts[i].ToLower()))
                        {
                            move = true;
                        }
                        else
                        {
                            move = false;
                            break;
                        }
                    }

                    if (move)
                    {
                        try
                        {
                            File.Move(file.FullName, serie.LocalDirectory+"\\"+file.Name);
                            returnResult.MovedSeries.Add(new FileMovingSerieResponseObject(serie.Title, file.Name));
                        }
                        catch (Exception)
                        {
                            returnResult.FailedSeries.Add(new FileMovingSerieResponseObject(serie.Title, file.Name));
                        }
                    }
                }

                return returnResult;
            }
            return new FileMovingResult();
        }
    }
}
